/**
 * @Title: ShareWXUtil.java
 * @author zengshuai shi
 * @date 2015年8月5日 下午6:05:42
 */
package com.shanghaionstar.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.BitmapUtil;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.PayMeConstants;
import com.onstar.cn.map.common.PoiItemInfo;
import com.shanghaionstar.R;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

/**
 * @author 7828wc updated by 2015-10-29
 * @ClassName: ShareWXUtil
 * @Description: 分享微信
 * @date 2015年8月5日 下午6:05:42
 */
public class ShareWXUtil {
    private static LogManager logger = LogManager.getInstance();

    public boolean shareScreenToFriend(Activity activity, IWXAPI mWeiXinApi, int scene, View view) {
//		WXWebpageObject webObject = new WXWebpageObject();
        WXImageObject imgObj = new WXImageObject(CommonUtil.getScreenShoot(activity));
//		String url = "http://www.shanghaionstar.com/ShareService/?";
//		String urlargs = json;
//		webObject.webpageUrl = url + urlargs;
//		logger.d("webpage.webpageUrl =" + webObject.webpageUrl);
        WXMediaMessage msg = new WXMediaMessage();
//		msg.mediaObject = webObject;
        msg.mediaObject = imgObj;
//		Bitmap thumb = BitmapFactory.decodeResource(context.getResources(),
//				R.drawable.share_wx_thumb);
//		msg.thumbData = BitmapUtil.bmpToByteArray(thumb, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");
        req.message = msg;
        req.scene = scene;
        req.openId = PayMeConstants.APP_ID;
        return mWeiXinApi.sendReq(req);
    }

    public boolean shareEvaluation(Context context,
                                   IWXAPI mWeiXinApi, int scene, String url, String title, String description, String imageUrl) {
        WXWebpageObject webObject = new WXWebpageObject();
        webObject.webpageUrl = url;
//        WXImageObject imageObject = new WXImageObject();
//        imageObject.imageUrl = imageUrl;
        WXMediaMessage msg = new WXMediaMessage();
        setThumbDataByUrl(msg, imageUrl);
        msg.mediaObject = webObject;
        msg.title = title;
        msg.description = description;
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.scene = scene;
        req.message = msg;
        req.openId = PayMeConstants.APP_ID;
        return mWeiXinApi.sendReq(req);
    }

    private void setThumbDataByUrl(WXMediaMessage msg, String imageUrl) {
        try {
            InputStream inputStream = new URL(imageUrl).openStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 80, 100, true);
            bitmap.recycle();//释放图像所占用的内存资源
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            thumbBmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
            msg.thumbData = baos.toByteArray();
            inputStream.close();
            baos.close();
        } catch (Exception ex) {
        }
    }

    public boolean shareToWX(Context context,
                             IWXAPI mWeiXinApi, int scene, String url, String title, String description) {
        WXWebpageObject webObject = new WXWebpageObject();
        webObject.webpageUrl = url;
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = webObject;
        msg.title = title;
        msg.description = description;
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        req.scene = scene;
        req.openId = PayMeConstants.APP_ID;
        return mWeiXinApi.sendReq(req);
    }

    public boolean shareToWX(Context context, IWXAPI mWeiXinApi, int scene, String url, String title, String description, int iconId) {
        WXWebpageObject webObject = new WXWebpageObject();
        webObject.webpageUrl = url;
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = webObject;
        msg.title = title;
        msg.description = description;
        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(),
                iconId);
        msg.thumbData = BitmapUtil.bmpToByteArray(thumb, true);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        req.scene = scene;
        req.openId = PayMeConstants.APP_ID;
        return mWeiXinApi.sendReq(req);
    }


    public boolean shareToFriend(Context context, PoiItemInfo poiItemInfo,
                                 IWXAPI mWeiXinApi, int scene) {
        WXWebpageObject webObject = new WXWebpageObject();
        String url = "http://www.onstar.com.cn/ShareService/?";
//		String url = "http://www.shanghaionstar.com/ShareService/?";
        String urlargs = "longitude=" + poiItemInfo.getLongitude()
                + "&latitude=" + poiItemInfo.getLatitude() + "&poiName="
                + poiItemInfo.getTitle() + "&address="
                + poiItemInfo.getAddress() + "&phoneNumber="
                + poiItemInfo.getTelphone();
        webObject.webpageUrl = url + urlargs;
        logger.d("webpage.webpageUrl =" + webObject.webpageUrl);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = webObject;
        msg.title = poiItemInfo.getTitle();
        msg.description = poiItemInfo.getAddress() + "\n"
                + poiItemInfo.getTelphone();
        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.share_wx_thumb);
        msg.thumbData = BitmapUtil.bmpToByteArray(thumb, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        req.scene = scene;
        req.openId = PayMeConstants.APP_ID;
        return mWeiXinApi.sendReq(req);
    }


    public boolean shareBitmapImg(
            IWXAPI mWeiXinApi, int scene, Bitmap bmp) {
        WXImageObject imgObj = new WXImageObject(bmp);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 80,80, true);
        bmp.recycle();
        msg.thumbData = bmpToByteArray(thumbBmp, true);  // 设置缩略图

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");
        req.message = msg;
        req.scene = scene;
        return mWeiXinApi.sendReq(req);
    }

    /**
     * @param @param  type
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: buildTransaction
     * @Description: 分享内容 的标识-text
     * 字符串 img-图片
     * 其他类型参照微信官方demo
     */
    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis())
                : type + System.currentTimeMillis();
    }

    /**
     * 缩略图
     *
     * @param bmp
     * @param needRecycle
     * @return
     */
    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
